package com.nbx.app.neheport.render;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.nbx.app.neheport.R;
import com.nbx.library.glutes.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class Render11 extends RenderBase {   
    private int mTextureID[];

    
	private static final int rows = 40; // 20 * 2

	private float vertices[] = new float[rows * 2 * 3];
	
	private float tex[] = new float[rows * 2 * 2];

	private static byte indices[] = new byte[rows * 2];
	
    private FloatBuffer mVetexBuffer;
    private FloatBuffer mTexBuffer;
    private ByteBuffer mIndexBuffer;
    
	private float xrot = 0.0f; // X Rotation
	private float yrot = 0.0f; // Y Rotation
	private float zrot = 0.0f; // Z Rotation

    public Render11(Context context) {
        super(context);
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        loadBitmapTex(gl);
        
		gl.glEnable(GL10.GL_TEXTURE_2D); // Enable Texture Mapping
		gl.glShadeModel(GL10.GL_SMOOTH); // Enable Smooth Shading
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
		gl.glClearDepthf(1.0f); // Depth Buffer Setup
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); // Really Nice Perspective Calculations
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE); // Set The Blending Function For Translucency
		gl.glEnable(GL10.GL_BLEND);

		int p = 0;
		for (int i = 0; i < rows; i++) {
			vertices[p + 0] = -2.0f;
			vertices[p + 3] = 2.0f; // x
			
			vertices[p + 1] = vertices[p + 4] = i * 4.0f / rows - 2.0f; // y
			vertices[p + 2] = vertices[p + 5] = (float) Math.sin((((i / 5.0f) * 22.50f) / 360.0f) * 3.141592654 * 2.0f);

			p += 6;
		}
		
		p = 0;
		for (int i = 0; i < rows; i++) {
			tex[p + 0] = 0.0f; 		tex[p + 2] = 1.0f;
			tex[p + 1] = tex[p + 3] = (i)/40.0f;
			
			p += 4;
		}	
		
		p = 0;
		for (int i = 0; i < rows; i++) {
			indices[p + 0] = (byte)(p + 1);
			indices[p + 1] = (byte)p;

			p += 2;
		}		
		
        mVetexBuffer = util.createFloatBuffer(vertices);
        mTexBuffer = util.createFloatBuffer(tex);	
        mIndexBuffer = util.createByteBuffer(indices);	
	}
    
    public void onDrawFrame(GL10 gl) {        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID[0]); // Select Our Texture
        
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f,0.0f,-5.0f);
  	  
        gl.glRotatef(xrot,1.0f,0.0f,0.0f);
        gl.glRotatef(yrot,0.0f,1.0f,0.0f);  
        gl.glRotatef(zrot,0.0f,0.0f,1.0f);
    	
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVetexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexBuffer);

		gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, rows * 2,
				GL10.GL_UNSIGNED_BYTE, mIndexBuffer);

        // TODO
        
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        
        xrot += 2.0f;
        yrot += 3.0f;
        zrot += 4.0f;
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
		gl.glViewport(0, 0, width, height); // Reset The Current Viewport

		gl.glMatrixMode(GL10.GL_PROJECTION); // Select The Projection Matrix
		gl.glLoadIdentity(); // Reset The Projection Matrix

		// Calculate The Aspect Ratio Of The Window
		GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,100.0f);

		gl.glMatrixMode(GL10.GL_MODELVIEW); // Select The Modelview Matrix
		gl.glLoadIdentity();
    }
    
    private void loadBitmapTex(GL10 gl){
        mTextureID = new int[1];
        gl.glGenTextures(1, mTextureID, 0);

        Bitmap bmp = util.getTextureFromBitmapResource(mContext, R.drawable.tim);

        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID[0]);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
        
        bmp.recycle();

        return;
    }
    
    @Override
    public boolean handleTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {

            return true;
        }
        return false;
    }

    @Override
    public boolean handleKeyEvent(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP) {

        }
        return false;
    }
}
